﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Employee
    {
        private ISessionFactory _sessionFactory;
        public Employee()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Employee)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListQuickSearch(string condition)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "select * from Employee where Last_Name + ' ' + First_Name like N'%" + condition + "%' and Status = 'CUR'";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Employee));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.Employee GetObjectByPK(string empId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Employee where Employee_ID =? and Status = 'CUR'").AddEntity(typeof(DataObjects.Employee));
                query.SetString(0, empId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Employee)arrTemp[0];
                }
                return null;
            }
        }

        public int GetMaxID()
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Employee order by Employee_Id desc").AddEntity(typeof(DataObjects.Employee));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                DataObjects.Employee oEmp;

                if (arrTemp.Count > 0)
                {
                    oEmp = (DataObjects.Employee)arrTemp[0];
                    if (oEmp != null)
                    {
                        return int.Parse(oEmp.EmployeeId);
                    }
                }
                return 0;
            }
        }

        public ArrayList SearchEmployee(object[] oParam)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "exec sp_SearchEmployee ?, ?, null, null, ?, null, null, null, ?";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Employee));
                query.SetString(0, oParam[0].ToString());                   //Employee_Id
                query.SetString(1, oParam[1].ToString());                   //Full_Name
                query.SetString(2, oParam[2].ToString());                   //Gender
                query.SetString(3, oParam[3].ToString());                   //Status

                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.Employee oEmp)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oEmp);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Employee oEmp)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oEmp);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string empId)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.Employee oEmp = (DataObjects.Employee)session.Load(typeof(DataObjects.Employee), empId);
                session.Delete(oEmp);
                tx.Commit();
            }
        }
    }
}
